from wormbane.model.item import Item
from wormbane.model.material import is_metallic_material
from wormbane.util.format import material_adjective

_default_materials = dict(armor = 'steel',
                          boots = 'leather',
                          cloak = 'cloth',
                          gloves = 'leather',
                          helm = 'steel',
                          shield = 'steel',
                          shirt = 'cloth')

class Armor(Item):
    is_armor = True
    kind = 'armor'
    slot = 'armor'
    beatitude = 'random'
    enchantable = True
    enchantment = 'random'
    power = None
    defense = 0
    protection = 0
    char = ']'
    desc_attrs = ('beatitude_prefix', 'enchantment_prefix', 'pair_prefix',
                  'color', 'material_prefix', 'structure_prefix', 'kind',
                  'power_suffix')
    worn = False
    material = 'steel'

    def __init__(self, **kwargs):
        kind = kwargs.get('kind', 'armor')
        kwargs.setdefault('material', default_armor_material(kind))
        kwargs.setdefault('slot', kind)
        Item.__init__(self, **kwargs)

    @property
    def pair_prefix(self):
        if is_paired_slot(self.slot):
            yield 'pair of'

    @property
    def material_prefix(self):
        # Steel is implied by the metallic structure prefixes.
        if (self.material != default_armor_material(self.kind)
            and not (self.material == 'steel'
                     and self.kind in ('armor', 'shirt'))):
            yield material_adjective(self.material)

    @property
    def structure_prefix(self):
        if is_metallic_material(self.material):
            if self.slot == 'armor':
                yield 'plate'
            elif self.slot == 'shirt':
                yield 'mail'

    @property
    def power_suffix(self):
        if self.power:
            yield 'of'
            yield self.power

    @property
    def wearer(self):
        return self.env if self.worn else None

def default_armor_material(kind):
    return _default_materials[kind]

def is_paired_slot(slot):
    return slot in ('boots', 'gloves')
